package com.tuo51.observation.controller;

import com.tuo51.core.domain.org.Classes;
import com.tuo51.core.domain.org.Organization;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.ClassesService;
import com.tuo51.core.service.StaffService;
import com.tuo51.core.vo.ClassesVO;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.evaluate.vo.children.ChildrenVO;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/oapi/classes")
@Api(value = "/oapi/classes", tags = "观测点 - 班级")
public class OBClassesController {


    @Autowired
    private ClassesService classesService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private StaffService staffService;

    @ApiOperation(value = "我的班级",httpMethod = "POST")
    @RequestMapping("/my_list")
    public StatusResponse my_list(@RequestHeader(TICKET) String ticket) {
        String staffId = cache.getStaffId(ticket);

        List<Classes> classesList = classesService.getClassesByStaff(staffId, false);

        List<ClassesVO> result = classesList.stream().map(k->ClassesVO.of(k)).collect(Collectors.toList());

        return StatusResponse.success(result);
    }

    @ApiOperation(value = "我的班级, 包含逻辑删除的对象",httpMethod = "POST")
    @RequestMapping("/my_full_list")
    public StatusResponse my_full_list(@RequestHeader(TICKET) String ticket) {
        String staffId = cache.getStaffId(ticket);

        List<Classes> classesList = classesService.getClassesByStaff(staffId, null);

        List<ClassesVO> result = classesList.stream().map(k->ClassesVO.of(k)).collect(Collectors.toList());

        return StatusResponse.success(result);
    }

    @ApiOperation(value = "班级成员",httpMethod = "POST")
    @RequestMapping("/members")
    public StatusResponse members(@RequestHeader(TICKET) String ticket, String classesId) {
        String staffId = cache.getStaffId(ticket);
        List<Children> childrenList = classesService.getMemberByClassesId(staffId, classesId);
        List<ChildrenVO> result = childrenList.stream().map(k->ChildrenVO.of(k)).collect(Collectors.toList());
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "添加成员，多个 ids 用竖线分割",httpMethod = "POST")
    @RequestMapping("/add_members")
    public StatusResponse addMembers(@RequestHeader(TICKET) String ticket, String classesId, String idsStr) {
        String staffId = cache.getStaffId(ticket);
        String[] ids = idsStr.split("\\|");
        boolean ret = classesService.addMembersToClassesId(staffId, ids, classesId);
        return StatusResponse.success(ret);
    }


    @ApiOperation(value = "移除班级成员",httpMethod = "POST")
    @RequestMapping("/remove_member")
    public StatusResponse removeMember(@RequestHeader(TICKET) String ticket, String classesId, String childrenId) {
        String staffId = cache.getStaffId(ticket);
        boolean ret = classesService.removeMemberToClassesId(staffId, childrenId, classesId);
        return StatusResponse.success(ret);
    }


    @ApiOperation(value = "创建或修改班级",httpMethod = "POST")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse edit(@RequestHeader(TICKET) String ticket,
                               String classesId,
                               String[] childrenIds,
                               String name, String remark, Integer limitNum, Integer beginMonth, Integer endMonth) {
        Staff staff = cache.getStaff(ticket);
        Organization org = staffService.staffOrgnization(staff);

        Classes classes = classesService.saveClasses(classesId, name, org.getId() , childrenIds, remark,
                limitNum, null, staff.getId(), 0,
                beginMonth, endMonth);

        return StatusResponse.success(classes);
    }

    @ApiOperation(value = "班级详情",httpMethod = "POST")
    @RequestMapping("/detail")
    @ResponseBody
    public StatusResponse detail(@RequestHeader(TICKET) String ticket, String classesId) {
        String staffId = cache.getStaffId(ticket);

        Classes response = classesService.getClassByStaff(staffId, classesId);

        return StatusResponse.success(response);
    }

    @ApiOperation(value = "禁用班级",httpMethod = "POST")
    @RequestMapping("/disable")
    @ResponseBody
    public StatusResponse disable(@RequestHeader(TICKET) String ticket,String classesId) {
        String staffId = cache.getStaffId(ticket);

        Boolean result = classesService.disable(staffId, classesId);
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "启用班级",httpMethod = "POST")
    @RequestMapping("/enable")
    @ResponseBody
    public StatusResponse enable(@RequestHeader(TICKET) String ticket, String classesId) {
        String staffId = cache.getStaffId(ticket);

        Boolean result = classesService.enable(staffId, classesId);
        return StatusResponse.success(result);
    }
}
